package com.hejia.alauda.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import static com.hejia.alauda.utils.Utils.*;
/**
 * 日期工具类
 * 提供日期时间辅助方法
 * 
 * @author hyr
 *
 */
public class DateTimeUtil {

	/**
	 * 获取日期时间的日期
	 * @param date	日期,格式yyyy-MM-dd
	 * @return		日期		
	 */
	public static Date getDateDay(String date){
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return dateFormat.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 获取日期时间的日期
	 * @param date	日期时间
	 * @return		日期		
	 */
	public static Date getDateDay(Date date){
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return dateFormat.parse(dateFormat.format(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 获取日期时间的当天最大时间的日期时间
	 * @param date	日期时间
	 * @return		当天最大时间的日期时间
	 */
	public static Date getDateAndMaxTime(Date date){
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
		SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		Date newDate = null;
		try {
			newDate = dateTimeFormat.parse(dateFormat.format(date) + " 23:59:59");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return newDate;
	}


    /**
     * 返回当前日期的昨日日期
     *
     * @return 日期的String类型
     */
    public static String getLastDay(){
        Calendar calendar = Calendar.getInstance();
        calendar.roll(Calendar.DATE, -1);
        Date createDate  = calendar.getTime();

        return new SimpleDateFormat("yyyy-MM-dd").format(createDate);
    }
    
    public static Date getMaxDate(Date date1, Date date2) {
		if (date1 != null && date2 != null) {
			return formatDate(date1).compareTo(formatDate(date2)) >= 0 ? date1
					: date2;
		}
    	return Calendar.getInstance().getTime();
    }
    
    public static Date getMinDate(Date date1, Date date2) {
		if (date1 != null && date2 != null) {
			return formatDate(date1).compareTo(formatDate(date2)) >= 0 ? date2
					: date1;
		}
    	return Calendar.getInstance().getTime();
    }
    
    public static String getMaxDateStr(String date1, String date2) {
		if (date1 != null && date2 != null) {
			return date1.compareTo(date2) >= 0 ? date1
					: date2;
		}
    	return formatDate(Calendar.getInstance().getTime());
    }
    
    public static String getMinDateStr(String date1, String date2) {
		if (date1 != null && date2 != null) {
			return date1.compareTo(date2) >= 0 ? date2
					: date1;
		}
		return formatDate(Calendar.getInstance().getTime());
    }
    
    public static Long getDaysBetween(Date startDate, Date endDate) {
		Calendar fromCalendar = Calendar.getInstance();
		fromCalendar.setTime(startDate);
		fromCalendar.set(Calendar.HOUR_OF_DAY, 0);
		fromCalendar.set(Calendar.MINUTE, 0);
		fromCalendar.set(Calendar.SECOND, 0);
		fromCalendar.set(Calendar.MILLISECOND, 0);

		Calendar toCalendar = Calendar.getInstance();
		toCalendar.setTime(endDate);
		toCalendar.set(Calendar.HOUR_OF_DAY, 0);
		toCalendar.set(Calendar.MINUTE, 0);
		toCalendar.set(Calendar.SECOND, 0);
		toCalendar.set(Calendar.MILLISECOND, 0);

		return (toCalendar.getTime().getTime() - fromCalendar.getTime().getTime()) / (1000 * 60 * 60 * 24);
	}
    
    /**
	 * 计算日期增加减少天数后的日期
	 * @param date
	 * @param i为负表示减多少天
	 * @return
	 */
	public static Date addDate(Date date,int i){
		if(date==null)return null;
		Calendar c = new GregorianCalendar();
		c.setTime(date);
		c.add(Calendar.DATE, i);
		return c.getTime();
	}
	
	 /**
     * 判断两个日期是否是同一天
     * 
     * @param date1
     *            date1
     * @param date2
     *            date2
     * @return
     */
    public static boolean isSameDate(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);

        boolean isSameYear = cal1.get(Calendar.YEAR) == cal2
                .get(Calendar.YEAR);
        boolean isSameMonth = isSameYear
                && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
        boolean isSameDate = isSameMonth
                && cal1.get(Calendar.DAY_OF_MONTH) == cal2
                        .get(Calendar.DAY_OF_MONTH);

        return isSameDate;
    }

}
